import 'package:flutter/services.dart';
import 'package:common_utils/common_utils.dart' show LogUtil;
import 'package:testlzhu/page/login/login_logic.dart';

class InputFormatter extends TextInputFormatter {
  String regExp = r"^((\-|\+)?(\d+)?)$";
  String phone = '^((13[0-9])|(15[^4])|(166)|(17[0-8])|(18[0-9])|(19[8-9])|(147,145))\\d{8}\$';
  String temp = "";

  int aType = 0;

  late LoginLogic controlor;

  InputFormatter(int type, LoginLogic loginLogic) {
    controlor = loginLogic;
    aType = type;
    switch (aType) {
      case 1:
        temp = phone;
        break;

      default:
        temp = regExp;
        break;
    }
  }

  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    if (newValue.text.isNotEmpty) {
      if (aType == 0) {
        if (RegExp(temp).firstMatch(newValue.text) != null) {
          return newValue;
        }
      } else {
        if (newValue.text.length == 11) {
          if (RegExp(temp).hasMatch(newValue.text) && newValue.text.length == 11) {
            LogUtil.e('符合${newValue.text}');
            controlor.setPhotnTip(true);
            return newValue;
          }
        } else if (newValue.text.length == 12) {
          controlor.setPhotnTip(true);
          return newValue;
        }else{
          controlor.setPhotnTip(false);
          LogUtil.e('不符合${newValue.text}');
          return newValue;
        }


      }
    }
    LogUtil.e('最后${newValue.text}');
    return newValue;
  }
}
